#include "songmenu.h"
#include "commonModel/songmodel.h"
#include "playListModel/myplaylistmodel.h"
#include "EZTally/EZTallyArray.h"

SongMenu::SongMenu(QWidget *parent) :
    QMenu(parent)
{
}







SongAddToMyPlayListMenu::SongAddToMyPlayListMenu(QWidget *parent) :
    QMenu(parent)
{
    _song = 0;
    EZTally::EZTallyAutoPtr ptr = MyPlayListModel::shareMyPlayList();
    _playLists = (EZTally::EZTallyArray<MyPlayListModel*>*)ptr.Get();
    _playLists->Retain();

    for(int i=0;i<_playLists->Count();i++){
        MyPlayListModel *mpm = _playLists->ObjectAtIndex(i);
        QAction *action = addAction(mpm->name());
        _actions.append(action);
    }
}
SongAddToMyPlayListMenu::~SongAddToMyPlayListMenu()
{
    setSong(0);
    _playLists->Release();
}

SongModel *SongAddToMyPlayListMenu::song() const
{
    return _song;
}

void SongAddToMyPlayListMenu::setSong(SongModel *song)
{
    if(song){
        song->Retain();
    }
    if(_song){
        _song->Release();
    }
    _song = song;
}

int SongAddToMyPlayListMenu::runActionEvent(QAction *action)
{
    if(_song==0){
        return -1;
    }
    int index = _actions.indexOf(action);
    if(index<0){
        return -2;
    }

    MyPlayListModel *mpm = _playLists->ObjectAtIndex(index);
    mpm->addSong(_song,true);

    return 0;
}

